/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_necromancy.h

	$Header: /game/dialog_necromancy.h $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_necromancy.h"

#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "button.h"
#include "button_cache.h"
#include "creature_icon_window.h"
#include "creature_select_window.h"
#include "external_string.h"
#include "format_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "interface_help.h"
#include "skill.h"
#include "skill_button.h"
#include "skill_properties.h"
#include "standard_fonts.h"
#include "text_window.h"

static t_bitmap_group_cache const	k_bitmaps( "dialog.necromancy" );
extern t_button_cache				g_ok_button;

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
t_dialog_necromancy::t_dialog_necromancy( t_window* parent )
                   : t_dialog_box( parent )
{
	m_layout = k_bitmaps.get();
	m_skill_frame = k_skill_frames.get();
	m_necromancer = 0;
	m_priest = 0;
	m_resurrected_creatures = 0;
}

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
void t_dialog_necromancy::open()
{
	t_screen_point	origin(0,0);
	t_screen_rect	necromancy_rect;
	t_screen_rect	resurrection_rect;
	t_screen_rect	window_rect(0,0,0,0);
	std::string		frame_string;

	if (m_necromancer != 0)
		frame_string = "necromancy";
	if (m_priest != 0)
	{
		if (!frame_string.empty())
			frame_string += "_and_";
		frame_string += "resurrection";
	}
	window_rect = m_layout->find( frame_string )->get_rect();
	if (m_priest != 0)
	{
		create_resurrection( window_rect.top_left()  );
	}
	if (m_necromancer != 0)
	{
		create_necromancy( -window_rect.top_left() );
	}

	// create close button.
	t_screen_rect   rect;
	t_button*		button;
	t_help_block const&		help = get_help_block( "shared" );

	rect = m_layout->find( "ok_button_" + frame_string )->get_rect() - window_rect.top_left();
	button = new t_button( g_ok_button.get(), rect.top_left(), this );
  set_help( button, help, "ok" );
	add_close_button( button, 0 );

	// center window
	window_rect -= window_rect.top_left();
	window_rect += (get_parent()->get_client_rect().size() - window_rect.size()) / 2;
	open_around( window_rect );
}

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
void t_dialog_necromancy::set_necromancy( t_hero* necromancer, t_creature_stack* creatures )
{
	m_necromancer = necromancer;
	m_necromancy_creatures = creatures;
}

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
void t_dialog_necromancy::set_resurrection( t_hero* priest, t_creature_array const& creatures )
{
	m_priest = priest;
	m_resurrected_creatures = &creatures;
}

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
void t_dialog_necromancy::create_item( t_screen_point const&	origin,
									   std::string const&		keyword,
									   t_skill_type				skill_type,
									   t_hero const*			hero,
									   std::string const&       label_text )
{
	t_screen_rect rect;
	t_skill       skill;

	// create icon
	rect = m_layout->find( keyword + "_skill_frame" )->get_rect();
	skill.skill = skill_type;
	skill.level = hero->get_skill( skill_type );
	if (skill.level < k_mastery_basic)
		skill.level = k_mastery_basic; // may be true if an artifact is being used
	create_framed_skill( skill, rect.top_left() + origin, m_skill_frame, this );

	// create icon label
	t_bitmap_layer const* layer;
	t_window*			  window;
	t_text_window*		  text_window;

	layer = m_layout->find( keyword + "_skill_background" );
	window = new t_bitmap_layer_window( layer, origin, this );
	rect = m_layout->find( keyword + "_skill_text" )->get_rect() + origin;
	text_window = new t_text_window( get_font( rect.height() / 2 ), rect,
		                             this, get_name( skill ), t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( false );
	text_window->set_center_horizontal();

	// create text
	std::string text;

	layer = m_layout->find( keyword + "_text_background" );
	window = new t_bitmap_layer_window( layer, origin, this );

	text = label_text;
	text = replace_keywords( text, hero );
	rect = m_layout->find( keyword + "_text" )->get_rect() + origin;
	text_window = new t_text_window( get_font( 20 ), rect, this, text, t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( false );
	text_window->set_center_horizontal();
}

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
void t_dialog_necromancy::create_item( t_screen_point const&	origin,
									   std::string const&		keyword,
									   t_skill_type				skill_type,
									   t_hero const*			hero,
									   t_creature_stack const*	creature, 
									   std::string const&       label_text )
{
	t_screen_rect rect;

	// create creature image
	t_screen_rect			frame_rect;
	t_screen_point			ring_origin;
	t_bitmap_layer const*	layer;
	t_window*				window;

	frame_rect = m_layout->find( keyword + "_creature" )->get_rect() + origin;
	window = new t_creature_icon_window( frame_rect, this, creature );

	// create label
	std::string		text;
	std::string		creature_text;
	t_text_window*	text_window;

	creature_text = format_string( "%i ", creature->get_number()  );
	creature_text += creature->get_name();
	layer = m_layout->find( keyword + "_creature_background" );
	window = new t_bitmap_layer_window( layer, origin, this );
	rect = m_layout->find( keyword + "_creature_text" )->get_rect() + origin;
	text_window = new t_text_window( get_font( rect.height() / 2 ), rect, this, creature_text,
		                             t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( false );
	text_window->set_center_horizontal();
	text_window->set_center_vertical();

	// create text
	text = label_text;
	text = replace_keywords( text, "%creatures", creature_text );
	create_item( origin, keyword, skill_type, hero, text );
}

// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
static t_external_string const k_text_necromancy( "necromancy.misc" );

void t_dialog_necromancy::create_necromancy( t_screen_point const& origin )
{
	create_item( origin, "necromancy", k_skill_necromancy, m_necromancer, 
		         m_necromancy_creatures, k_text_necromancy );
}


// --------------------------------------------------------------------
// dialog to display resurrection, summoning, and necromancy results
// --------------------------------------------------------------------
static t_external_string const k_text_resurrection( "resurrection.misc" );

void t_dialog_necromancy::create_resurrection( t_screen_point const& origin )
{
	assert( m_resurrected_creatures != 0 );
	create_item( origin, "resurrection", k_skill_resurrection, m_priest, k_text_resurrection );

	// set creatures
	int							i;
	t_creature_select_window*	creature_window;
	t_screen_rect				rect;
	int							count;

	rect = m_layout->find( "resurrection_creatures" )->get_rect() + origin;
	creature_window = new t_creature_select_window( rect.top_left(), false, this );
	for (i = 0; i < t_creature_array::k_size; ++i)
	{
		t_creature_stack const& stack = (*m_resurrected_creatures)[i];
		if (stack.get_number() > 0)
		{
			count = creature_window->get_count();
			creature_window->add( &stack );
			creature_window->set_help_text( count, stack.get_name() );
		}
	}
}

